<!--  -->
<template>
  <div>
    <van-form @submit="changeFromData" @failed="failedMethod">

      <!-- 头 -->
      <div class="font15 mt10 pl10">
        <span class="titletext">{{ nFromdata.title }}</span>
        <div class="graytext mt10 font13">
          <span>表单编号：</span>
          <span class="titletext">{{ nFromdata.pmInsId }}</span>
        </div>
      </div>


      <van-tabs v-model="active" color="#1989fa" sticky animated line-width="33%" offset-top="50">

        <van-tab title="基础信息">
          <XeTableOne ref="toMsgs" :fromdata='fromdata' :showHistory='showHistory' :approveList='approveList'
            :historydata='historydata' :location="params.location" :workItemName="params.workItemName"
            @freshDat='freshData($event)' />
        </van-tab>

        <van-tab title="其他信息">
          <XeTableTwo :fromdata='fromdata' :zhenbieData="zhenbieData" />
        </van-tab>
      </van-tabs>


      <!-- 跳转意见 -->
      <div class="left_botton">
        <button block type="button" @click="tomsg()" style="color:#60a9ec; display: inline-block;width:100%;border: 0px solid #60a9ec;border-radius: 15px 0px 0px 15px;
            padding:10px 10px 10px 15px;font-size:13px">意见</button>
      </div>


      <!-- 流转下一步 -->
      <div v-if="todotype||toRead" class="footer">
        <van-button block type="info" native-type="submit" :disabled="!canToNext"
          style="display: inline-block; width: 100%; border: 0px solid #60a9ec">{{ buttonTitle }}</van-button>
      </div>

    </van-form>

  </div>
</template>



<script>
import {
  getFormDetail,
  getDecisionByPmInsId,
  getWfLinkTracks,
  getWfOptMags,
  findAllNoPage,
  getLocationNameById,
} from "@/api/dictapi";

import { mapGetters } from "vuex";
import { Notify, Toast } from "vant";
import { openFileUrl } from "@/store/openFile";
import XeTableOne from "./xeTableOne.vue";
import XeTableTwo from "./xeTableTwo.vue";
import util from "@/assets/js/public";



export default {
  components: { XeTableOne, XeTableTwo },
  data() {
    return {

      fromdata: {},
      nFromdata: {},
      zhenbieData: {},

      showHistory: false,
      historydata: {},
      approveList: [],

      params: this.$route.query,
      todotype: false,
      active: 0,
      toRead: false,
      buttonTitle: '流转下一步',
      canToNext: true,
    };
  },
  computed: {
    ...mapGetters(["user"]),
    ...mapGetters(["userinfo"]),
  },

  //生命周期--创建完成
  created() {
    if (this.params.pmInsType == null && this.params.processType != null) {
      this.params.pmInsType = this.params.processType
    }
    if (this.params.todoType == "1") {
      this.todotype = true;
    }
    else if (this.params.todoType == "2") {
      this.showHistory = true;
    }else if (this.params.todoType == "3") {
      this.toRead = true;
    }
    
    if (this.params.location == 'dict.start') {
      this.canToNext = false;
      this.buttonTitle = '暂不支持该环节,请至PC端办理';
    }

    this.getdetail();
  },

  //生命周期--挂载完成
  mounted() { },

  //方法调用
  methods: {

    failedMethod(data) {

      console.log('submit fail:', data);
      const errorArr = data.errors;
      const errorObj = errorArr[0];
      const errorMessage = errorObj.message;
      
      Toast({ type: 'text', message: errorMessage, duration: 1000 });
    },


    freshData(value) {


      this.fromdata = value;
      console.log('AAAAAAAAccccc' + this.fromdata.cityDecision);
    },

    tomsg() {

      this.active = 0;
      setTimeout(() => {
        document.getElementById('opinion').scrollIntoView({ behavior: 'smooth' });
      }, 350);
    },

    openFileUrlfun(value) {
      this.$router.push({
        name: "fileview",
        query: {
          openFileUrl: openFileUrl(value),
        },
      });
    },


    // 获取详情数据
    getFromData() {

      getDecisionByPmInsId(Object.assign(this.params, { userName: this.user.userName })).then((result) => {
        this.fromdetal = {};
        Toast.clear();
        if (!result.data) {
          Notify(result.message);
          return;
        }

        if (result.data.data) {

          this.fromdata = result.data.data;
          this.getRHNLZBMessage(this.fromdata.businessId);
        }
      });
    },

    getdetail() {
      Toast.loading({ message: "加载中", forbidClick: false, });
      getFormDetail(Object.assign(this.params, { userName: this.user.userName })).then((result) => {
        this.fromdetal = {};
        Toast.clear();
        if (!result.data) {
          Notify(result.message);
          return;
        }

        if (result.data.data) {
          this.nFromdata = result.data.data;
        }
      });


      getLocationNameById(Object.assign(this.params, { userName: this.user.userName })).then((result) => {

        if (!result.data) {
          Notify(result.message);
          return;
        }

        if (result.data.data) {

          this.params.workItemName = result.data.data.workItemName;
          this.getFromData();
        }
      });

      getWfOptMags(Object.assign(this.params), {
        userName: this.user.userName,
      }).then((result) => {
        Toast.clear();
        if (!result.data) {
          Notify(result.message);
          return;
        }
        this.approveList = result.data.data.reverse();
      });


      if (this.showHistory) {
        getWfLinkTracks(Object.assign(this.params), {
          userName: this.user.userName,
        }).then((result) => {
          Toast.clear();
          if (!result.data) {
            Notify(result.message);
            return;
          }
          this.showHistory = false;
          if (result.data.data) {
            //数据过滤，并生成新的数组
            this.historydata = result.data.data.filter((item) => item.CURRENTSTATE == 10)[0];
            if (this.historydata) {
              if (this.historydata.MANAGER) {
                this.showHistory = true;
              }
            }
          }
        });
      }
    },

    /// 融合能力甄别
    getRHNLZBMessage(value) {
      findAllNoPage(Object.assign(this.params, { userName: this.user.userName, businessId: value })).then((result) => {
        this.fromdetal = {};
        Toast.clear();
        if (!result.data) {
          Notify(result.message);
          return;
        }
        if (result.data.data) {
          this.zhenbieData = result.data.data;
        }
      });
    },

    /// 
    changeFromData() {
      //提交流转
      // console.log("传给递交页面:", this.params)
      // console.log("formDataformData", this.fromdata);

      var formSendData = { "htmlParamMap": {} };
      formSendData.htmlParamMap = this.fromdata;
      formSendData.id = this.nFromdata.id;
      formSendData.pmInsId = this.nFromdata.pmInsId;
      formSendData.processType = this.params.pmInsType
      formSendData.workItemId = this.params.workItemId;

      var params1 = {
        kv: {
          currentUserCode: util.encrypt(this.params.userName),
          source: 'MOBILE',
          location: this.params.location,
          workItemName: this.params.workItemName,
          processInstId: this.params.processInstId,
          processDefName: this.params.processDefName,
          processType: this.params.pmInsType,
        },

        body: {
          location: this.params.location,
          processInstId: this.params.processInstId,
          processDefName: this.params.processDefName,
          processType: this.params.pmInsType,
        }
      };

      var params2 = {
        kv: {
          "currentUserCode": util.encrypt(this.params.userName),
          "source": 'MOBILE',
          "location": this.params.location,
          "processInstId": this.params.processInstId,
          'processType': this.params.pmInsType,
          'receiptCode': this.params.processInstId,
          'provinceSupport': this.fromdata.provinceSupport,
        },
      };

      var params3 = {
        kv: {
          "source": 'MOBILE',
          "currentUserCode": util.encrypt(this.params.userName),
          "workItemId": this.params.todoType==3?"":this.params.workItemId,
          "processInstId": this.params.processInstId,
          "location": this.params.location,
          "notificationId":this.params.todoType==3?this.params.wfnid:""
        },
      };

      /// 承办模块 当前可操作的
      var isCityOrProvince = null;
      var canEditDSCB = this.$refs.toMsgs.canEditDSCB;
      var canEditSGSCB = this.$refs.toMsgs.canEditSGSCB;
      if (canEditDSCB) {
        isCityOrProvince = 'city';
      }

      if (canEditSGSCB) {
        isCityOrProvince = 'province';
      }

      console.log(this.fromdata.provinceSupport);
      localStorage.setItem('submitPageParam', JSON.stringify({
          getDecisionsUrl: 'decisionAndUser$api$getDecisions',
          getUserUrl: 'decisionAndUser$api$getOrgAndUser',
          tosubmintUrl: 'applicationFormEnhance$api$startSubmitProcess',

          getDecisionsParams: params1,
          getUserParams: params2,
          tosubmintParams: params3,
          formData: formSendData,

          cityDecision: this.fromdata.cityDecision,
          provinceDecision: this.fromdata.provinceDecision,
          isCityOrProvince: isCityOrProvince,
          fromMoa: this.params.fromMoa,
          type:this.params.todoType==3?"toRead":"toDo"
        }));
      this.$router.push({
        name: "srqrsubmit"
      });
    },
  },

};
</script>
<style  scoped>
/* @import url();*/
</style>